﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       PansonicSerial
- 机器名称：       YTE
- 命名空间：       LD.Common.PLC
- 文 件 名：       PansonicSerial
- 创建时间：       2021/12/16 12:03:23
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.Host;
using LdCommunication;
using LdCommunication.ModBus;
using LdCommunication.Profinet.Panasonic;
using System;
using System.IO.Ports;
using System.Threading.Tasks;
namespace LD.PLC
{
    /// <summary>
    /// 西门子串口
    /// </summary>
    public class ModbusCom : PLCAbstract
    {
        private TaskEx taskEx = new TaskEx();
        private ModbusRtu _plc;
        private Status status = Status.失败;
        public override event ConnectionStatusDelegate StatusEvent;
        public override Status Status
        {
            get { return status; }
            set
            {
                status = value;
                StatusEvent?.BeginInvoke(Name, value, null, _plc);
            }
        }

        public ModbusCom()
        {
            _plc = new ModbusRtu(1);
        }
        private void Connection(object param)
        {
            if (Status == Status.失败)
            {
                OperateResult operate = _plc.Open();
                if (operate.IsSuccess)
                    Status = Status.成功;
            }
            Task.Delay(3000).Wait();
        }
        public override void Start()
        {
            if (HDParam != null)
            {
                Parity parity = Parity.Even;
                if (HDParam.Parity > -1)
                    parity = (Parity)Enum.Parse(typeof(Parity), HDParam.Parity + "");
                _plc.SerialPortInni(HDParam.Address, HDParam.Port, 8, StopBits.One, parity);
                _plc.ReceiveTimeout = HDParam.TimeOut;

                taskEx.Start(Connection, null);
            }
        }
        public override void Stop()
        {
            taskEx?.Cancel();
            _plc?.Close();
        }

        public ModbusRtu Ins
        {
            get
            {
                return _plc;
            }
        }
        public override OperateResult<byte[]> Read(string address, ushort val)
        {
            return _plc.Read(address, val);
        }

        public override OperateResult<bool[]> ReadBool(string address, ushort val)
        {
            return _plc.ReadBool(address, val);
        }
        public override OperateResult<float> ReadFloat(string address)
        {
            return _plc.ReadFloat(address);
        }

        public override OperateResult<bool> ReadBool(string address)
        {
            return _plc.ReadBool(address);
        }

        public override OperateResult<string> ReadString(string address, ushort val)
        {
            return _plc.ReadString(address, val);
        }

        public override OperateResult<int> ReadInt(string address)
        {
            return _plc.ReadInt32(address);
        }

        public override OperateResult<int[]> ReadInt(string address, ushort val)
        {
            return _plc.ReadInt32(address, val);
        }

        public override OperateResult<short[]> ReadInt16(string address, ushort val)
        {
            return _plc.ReadInt16(address, val);
        }

        public override OperateResult<short> ReadInt16(string address)
        {
            return _plc.ReadInt16(address);
        }

        public override OperateResult Write(string address, short val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, int val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, bool val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, string val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult Write(string address, byte[] val)
        {
            return _plc.Write(address, val);
        }

        public override OperateResult<float[]> ReadFloat(string address, ushort val)
        {
            return _plc.ReadFloat(address, val);
        }
        public override OperateResult Write(string address, float val)
        {
            return _plc.Write(address, val);
        }

        public override bool Wait(string address, short val, int timeout)
        {
            bool result = false;
            var time = _plc.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override bool Wait(string address, bool val, int timeout)
        {
            bool result = false;
            var time = _plc.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override OperateResult Write(string address, double val)
        {
            return _plc.Write(address, val);
        }
    }
}
